package com.flight.trans.activity;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flight.trans.R;
import com.flight.trans.global.Constant;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.LogUtils;
import com.flight.trans.view.PinchImageView;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.http.GlideLoader;
import com.flight.trans.listener.PagerOnPagerChangedListener;

import java.util.ArrayList;
import java.util.List;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * @author : YanKing
 * @date : 2019/7/16  15:46
 * @Description : 查看图片
 */
public class SeePicActivity extends BaseActivity {

    private RelativeLayout mRlContainer ;
    private ViewPager mViewPager;
    private TextView mTvPosition,//当前
            mTvTotal;//总数

    private ViewPagerAdapter mImageAdapter;

    private List<String> mPicList;
    private List<View> mImageViewList;

    private int mPosition ;

    @Override
    protected int setContentView() {
        return R.layout.activity_see_pic;
    }

    @Override
    protected void initView() {
        mRlContainer = findViewById(R.id.mRlContainer);
        mViewPager = findViewById(R.id.mViewPager);
        mTvPosition = findViewById(R.id.mTvPosition);//当前
        mTvTotal = findViewById(R.id.mTvTotal);//总数
    }

    @Override
    protected void initData() {
        if (mBundle != null) {
            mPicList = mBundle.getStringArrayList(Constant.INFO);
            mPosition = mBundle.getInt(Constant.POSITION);
        }

        mPicList.remove(null);

        mTvTotal.setText(mPicList.size() + "");
        mTvPosition.setText((mPosition + 1) + "");

        initAdapter();
    }

    private void initAdapter() {
        mImageViewList = new ArrayList<>();

        for (int i = 0; i < mPicList.size(); i++) {
            if(!TextUtils.isEmpty(mPicList.get(i))) {
                if(mPicList.get(i).endsWith("mp4")){
                    JZVideoPlayerStandard jzVideoPlayerStandard = (JZVideoPlayerStandard) LayoutInflater.from(mActivity).inflate(R.layout.view_video , null);
                    jzVideoPlayerStandard.setUp(mPicList.get(i) , JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL);
                    mImageViewList.add(jzVideoPlayerStandard);
                }else {
                    PinchImageView imageView = new PinchImageView(mActivity);
                    LogUtils.e("图片大" , mPicList.get(i));
                    GlideLoader.loader(mPicList.get(i), imageView);
                    mImageViewList.add(imageView);
                }
            }
        }

        // 初始化Adapter
        mImageAdapter = new ViewPagerAdapter();
        mViewPager.setAdapter(mImageAdapter);

        // 绑定回调,监听ViewPager的滑动事件
        mViewPager.setOnPageChangeListener(new PagerChangedListener());

        mViewPager.setCurrentItem(mPosition);
    }

    /**
     * ViewPage滑动监听
     */
    private class PagerChangedListener extends PagerOnPagerChangedListener {
        @Override
        public void onPageSelected(int position) {
            mTvPosition.setText((position + 1) + "");
        }
    }

    public class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImageViewList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return (arg0 == arg1);
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(mImageViewList.get(position));
        }

        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager) container).addView(mImageViewList.get(position), 0);

            return mImageViewList.get(position);
        }
    }

    public void onClick(View view){
        onFinish();
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }
    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }
}
